﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HQWeb.Resources.MultiMaterialList
{
    public partial class SelectList : BasePage
    {
        private static string strType;
        protected void Page_Load(object sender, EventArgs e)
        {
            //VerdictIsLogin();
            if (!this.IsPostBack)
            {                
                    CheckPara();
                    BindList();
            }
        }

        #region 参数判断
        private void CheckPara()
        {
            //this.txtObjTreeIDHid.Value = Request["objTreeID"];
            string strId = Request["objTreeID"];
            strType = "'" + strId + "'" + ",";
            GetAllType(strId);
            this.txtObjTreeIDHid.Value = strType.Substring(0, strType.Length - 1);
        }
        #endregion

        #region 获得所有下级类型
        private void GetAllType(string strID)
        {
            GetChild(strID);
        }
        #endregion

        #region 获取孩子节点
        private void GetChild(string strID)
        {
            BLL.SysManage.T_PUB_MATERIALTYPE bll = new BLL.SysManage.T_PUB_MATERIALTYPE();
            List<Model.SysManage.T_PUB_MATERIALTYPE> modelist = new List<Model.SysManage.T_PUB_MATERIALTYPE>();
            string strWhere = string.Format(" PARENT_ID = {0}", Convert.ToInt32(strID));
            modelist = bll.GetModelList(strWhere);
            foreach (Model.SysManage.T_PUB_MATERIALTYPE item in modelist)
            {
                strType += "'" + item.TYPE_ID + "'" + ",";
                GetChild(item.TYPE_ID.ToString());
            }
        }
        #endregion

        #region 绑定数据
        private void BindList()
        {
            //string strWhere = "";

            //if (this.txtSearchHid.Value != "" && this.txtSearchHid.Value != null)
            //{
            //    strWhere = this.txtSearchHid.Value;
            //}
            //DataSet ds = CreateData(strWhere);
            BLL.SysManage.T_PUB_MATERIAL bll = new BLL.SysManage.T_PUB_MATERIAL();
            DataSet ds = bll.GetMaterialInfo(this.txtObjTreeIDHid.Value, this.Titel.Text.Trim(), "2");
            //string strIDs = Session["Param"].ToString();
            //GridView1.DataSource = bll.GetMaterialInfo(this.txtObjTreeIDHid.Value, this.txtSearch.Value, "2");
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataBind();

            this.txtPageIDValueHid.Value = "";
            this.txtPageNameValueHid.Value = "";
            for (int j = 0; j < GridView1.Rows.Count; j++)
            {
                if (j != GridView1.Rows.Count - 1)
                {
                    this.txtPageIDValueHid.Value += GridView1.Rows[j].Cells[0].Text.Trim() + ",";
                    this.txtPageNameValueHid.Value += GridView1.Rows[j].Cells[1].Text.Trim() + ",";
                }
                else
                {
                    this.txtPageIDValueHid.Value += GridView1.Rows[j].Cells[0].Text.Trim();
                    this.txtPageNameValueHid.Value += GridView1.Rows[j].Cells[1].Text.Trim() + ",";
                }
            }

            this.txtAllIDValueHid.Value = "";
            this.txtAllNameValueHid.Value = "";

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (i != ds.Tables[0].Rows.Count - 1)
                {
                    this.txtAllIDValueHid.Value += ds.Tables[0].Rows[i]["MATERIAL_ID"].ToString() + ",";
                    this.txtAllNameValueHid.Value += ds.Tables[0].Rows[i]["MATERIAL_NAME"].ToString() + ",";
                }
                else
                {
                    this.txtAllIDValueHid.Value += ds.Tables[0].Rows[i]["MATERIAL_ID"].ToString();
                    this.txtAllNameValueHid.Value += ds.Tables[0].Rows[i]["MATERIAL_NAME"].ToString();
                }
            }

            BindDiv();
        }
        #endregion


        #region 绑定已选定
        private void BindDiv()
        {
            string strJS = "fnBindSelect();";
            Universal.JS.RunJS(strJS);
        }
        #endregion

        #region 翻页
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                int newPageIndex = 0;

                if (-2 == e.NewPageIndex)
                {
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = GridView1.Controls[0].Controls[GridView1.Controls[0].Controls.Count - 1] as GridViewRow;
                    if (null != pagerRow)
                    {
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                    }

                    if (null != txtNewPageIndex)
                    {
                        newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                    }

                    newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                    newPageIndex = newPageIndex >= GridView1.PageCount ? GridView1.PageCount - 1 : newPageIndex;
                    GridView1.PageIndex = newPageIndex;
                }
                else if (e.NewPageIndex == -1)
                {
                    newPageIndex = 0;
                    GridView1.PageIndex = 0;
                }
                else
                {
                    newPageIndex = e.NewPageIndex;
                    GridView1.PageIndex = e.NewPageIndex;
                }
                BindList();
            }
            catch (Exception ex)
            {
                Universal.JS.RunJS(ex.Message);
                return;
            }
        }
        #endregion

        #region 查询事件
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //this.txtSearchHid.Value = " USER_NAME like '%" + this.txtSearch.Value + "%' ";
            BindList();
        }
        #endregion

        #region 行绑定触发事件
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Visible = false; //隐藏列,可以取得该隐藏列的信息 
            }
        }
        #endregion
    }
}